export default class GameDataModel {
  public static _instance: GameDataModel = null;

  public static getInstance(): GameDataModel {
    if (this._instance == null) {
      this._instance = new GameDataModel();
    }
    return this._instance;
  }

  private _gameData = {
    //二维数组地图
    mapData: [[]],
    mapWidth: 256,
    mapHeight: 90,
  };

  public get mapData() {
    return this._gameData.mapData;
  }
  public set mapData(value) {
    this._gameData.mapData = value;
  }

  public get mapWidth() {
    return this._gameData.mapWidth;
  }

  public get mapHeight() {
    return this._gameData.mapHeight;
  }

  //生成地图
  public createNewMap() {
    this._gameData.mapData = [];
    for (let i = 0; i < this._gameData.mapWidth; i++) {
      this._gameData.mapData[i] = [];
      for (let j = 0; j < this._gameData.mapHeight; j++) {
        this._gameData.mapData[i][j] = 0;
      }
    }
  }

  //随机地图数据
  public randomMap() {
    let n = 1; //10代表初始化10 % 的数据

    let count = 100; // (this._gameData.mapWidth * this._gameData.mapHeight * n) / 100;
    for (let i = 0; i < count; i++) {
      let height = Math.floor(Math.random() * this._gameData.mapHeight);
      let width = Math.floor(Math.random() * this._gameData.mapWidth);
      this._gameData.mapData[height][width] = 1;
    }
  }
}
